﻿Imports NMock2

Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports Meiners.Libs.Withings
Imports Meiners.Libs.Withings.Core
 

'''<summary>
'''This is a test class for WithingsUserTest and is intended
'''to contain all WithingsUserTest Unit Tests
'''</summary>
<TestClass()> _
    Public Class UserTest

    Private testContextInstance As TestContext
    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property


    '''<summary>
    '''A test for UserID
    '''</summary>
    <TestMethod()> _
    Public Sub UserIDTest()
        Dim userID As Integer = New Random().Next(10000, 9999999)
        Dim publicKey As String = "asdf1234"
        Dim target As User = New User(userID, publicKey)
        Dim actual As Integer
        actual = target.UserId
        Assert.AreEqual(Of Integer)(userID, actual)
    End Sub
    '''<summary>
    '''A test for PublicKey
    '''</summary>
    <TestMethod()> _
    Public Sub PublicKeyTest()
        Dim userID As Integer = 12345
        Dim publicKey As String = Guid.NewGuid.ToString

        Dim target As User = New User(userID, publicKey)
        Dim actual As String
        actual = target.PublicKey
        Assert.AreEqual(Of String)(publicKey, actual)
    End Sub


    '''<summary>
    '''A test for WBSAPIConnector Constructor with test for given parameters
    '''</summary>
    <TestMethod()> _
    Public Sub Constructor_Success()
        Dim userID As Integer = 123456
        Dim publicKey As String = "asdf57ghj"
        Dim target As User = New User(userID, publicKey)

        Assert.IsNotNull(target, "Could not create an instance of User")

        Assert.AreEqual(Of Integer)(userID, target.UserId, "Property UserID is not correct")
        Assert.AreEqual(Of String)(publicKey, target.PublicKey, "Property publicKey is not correct")
    End Sub
    '''<summary>
    '''Construtor Test for first Parameter
    '''</summary>
    <TestMethod(), _
    ExpectedException(GetType(System.ArgumentNullException))> _
    Public Sub Constructor_Fail_Parameter_1()
        Dim userID As Integer = -1
        Dim publicKey As String = String.Empty
        Dim target As User = New User(userID, publicKey)
    End Sub
    '''<summary>
    ''' Constructor Test for second parameter
    '''</summary>
    <TestMethod(), _
    ExpectedException(GetType(System.ArgumentNullException))> _
    Public Sub Constructor_Fail_Parameter_2()
        Dim userID As Integer = 12354
        Dim publicKey As String = Nothing
        Dim target As User = New User(userID, publicKey)
    End Sub


    '''<summary>
    '''A test for SetPublic
    '''</summary>
    <TestMethod()> _
    Public Sub ReadMeasuresTest_Success()
        Dim userID As Integer = 29
        Dim publicKey As String = "b71d95d5fb963458"
        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As Withings.Core.IHttpConnector = Nothing
        Dim actual As GetMeasuresResponse

        Dim target As User = New User(userID, publicKey)

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        WBSAPIConnector.WebConnector = webMock

        Stub.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/measure?action=getmeas&userid=29&publickey=b71d95d5fb963458"}).Will([Return].Value(My.Resources.getMeasures_SampleResult))

        actual = target.ReadMeasures(Nothing, Nothing, Nothing)
        mocks.VerifyAllExpectationsHaveBeenMet()

        Assert.AreEqual(Of DateTime)(Helper.EPOCHToDateTime(1249409679), actual.UpdateTime)
        Assert.IsTrue(actual.ReadMeasureGroups.Count = 2, "MeasureGroups.Count is wrong")

        Dim selection As Collections.Generic.IEnumerable(Of Measure) = Nothing

        Assert.IsTrue(actual.ReadMeasureGroups(0).GetMeasures.Count = 4, "First MeasureCount is wrong")
        Assert.IsTrue(actual.ReadMeasureGroups(1).GetMeasures.Count = 1, "Second MeasureCount is wrong")


        Assert.AreEqual(Of Integer)(2909, actual.ReadMeasureGroups(0).GroupId)
        Assert.AreEqual(Of CategoryEnum)(CategoryEnum.IsMeasure, actual.ReadMeasureGroups(0).Category)
        Assert.AreEqual(Of DateTime)(Helper.EPOCHToDateTime(1222930968), actual.ReadMeasureGroups(0).MeasureGroupDate)
        Assert.AreEqual(Of AttributionStatusEnum)(AttributionStatusEnum.CapturedByDeviceBelongsToUser, actual.ReadMeasureGroups(0).AttributionStatus)
        Assert.AreEqual(Of MeasureTypeEnum)(MeasureTypeEnum.Heigth, actual.ReadMeasureGroups(1).GetMeasures(0).MeasureType)


    End Sub
    '''<summary>
    '''A test for SetPublic
    '''</summary>
    <TestMethod()> _
    Public Sub SetPublicTest_Success()
        Dim userID As Integer = 29
        Dim publicKey As String = "b71d95d5fb963458"
        Dim mocks As NMock2.Mockery = New NMock2.Mockery()
        Dim webMock As Withings.Core.IHttpConnector = Nothing

        Dim target As User = New User(userID, publicKey)
        Dim setBodyScaleDatePublic As Boolean = True
        Dim setBloodPressureMonitorDataPublic As Boolean = False

        webMock = CType(mocks.NewMock(GetType(IHttpConnector)), IHttpConnector)
        WBSAPIConnector.WebConnector = webMock

        Expect.Once.On(webMock).Method("GetWebserviceResponse").With(New String() {"http://wbsapi.withings.net/user?action=update&userid=29&publickey=b71d95d5fb963458&ispublic=1"}).Will([Return].Value(My.Resources.probe_SampleResult))
        target.SetPublic(setBodyScaleDatePublic, setBloodPressureMonitorDataPublic)
        mocks.VerifyAllExpectationsHaveBeenMet()

    End Sub

End Class
 